package com.cskaoyan.service;

import com.alibaba.druid.sql.visitor.functions.If;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.WxFootPrintListVo;
import com.cskaoyan.bean.wxbo.WxCouponListBo;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.List;

/**
 * @program:
 * @description:
 * @author: Qian Jin
 * @create: 2021-07-13
 * @detail:
 **/

@Service
public class WxFootprintServiceImpl implements WxFootprintService {

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public WxFootPrintListVo getFootprintList(WxCouponListBo listBo) {

        WxFootPrintListVo wxFootPrintListVo = new WxFootPrintListVo();

        //分页
        PageHelper.startPage(listBo.getPage(), listBo.getSize());

        //取出当前登陆的用户的id
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        //获取当前用户足迹列表
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(userId);
        footprintExample.setOrderByClause("update_time" + " " + "desc");
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);

        //足迹不为空 （新用户足迹为空）
        if (!footprints.isEmpty()) {
            //封装内部FootprintListDTO
            ArrayList<WxFootPrintListVo.FootprintListDTO> footprintListDTOS = new ArrayList<>();

            for (Footprint footprint : footprints) {
                WxFootPrintListVo.FootprintListDTO footprintListDTO = new WxFootPrintListVo.FootprintListDTO();
                //查出每条足迹对应的商品
                //这个商品可以是已经被删除的
                Integer goodsId = footprint.getGoodsId();
                Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
                footprintListDTO.setBrief(goods.getBrief());
                footprintListDTO.setGoodsId(goodsId);
                footprintListDTO.setName(goods.getName());
                footprintListDTO.setPicUrl(goods.getPicUrl());
                footprintListDTO.setRetailPrice(goods.getRetailPrice());
                footprintListDTO.setAddTime(footprint.getUpdateTime());
                footprintListDTO.setId(footprint.getId());

                footprintListDTOS.add(footprintListDTO);
            }
            wxFootPrintListVo.setFootprintList(footprintListDTOS);

            //总页数
            PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
            int pageSize = footprintPageInfo.getPageSize();

            wxFootPrintListVo.setTotalPages(pageSize);

            return wxFootPrintListVo;
        }
        //足迹为空，直接返回空对象
        return wxFootPrintListVo;
    }

    @Override
    public void deleteWxFootprint(Integer id) {
        Footprint footprint = new Footprint();
        footprint.setId(id);
        footprint.setDeleted(true);
        int i = footprintMapper.updateByPrimaryKeySelective(footprint);
    }
}
